﻿using Ninject;
using Radius.Admin.Core;
using Rmac.DataBase.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rmac.Service.Config
{
    public class SysConfigService
    {
        [Inject]
        private SqlSugarRepository<SysConfig> _configRepository { set; get; }


        public SysConfigService(SqlSugarRepository<SysConfig> configRepository) {
            _configRepository = configRepository;   
        }

        /// <summary>
        /// 获取参数列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<SysConfig>> GetConfigList(SysConfig config)
        {
            return await _configRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(config.Code), x => x.Code == config.Code)
            .ToListAsync();
        }

        /// <summary>
        /// 更新参数
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<Object> UpdateConfig(SysConfig config) {
            var entity = await _configRepository.GetFirstAsync(x => x.Id == config.Id);
            if (entity == null)
            {
                throw new Exception("此记录不存在！");
            }
            return await _configRepository.AsUpdateable(config).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据编码获取参数
        /// </summary>
        /// <returns></returns>
        public async Task<SysConfig> GetConfigByCode(string code)
        {
            var entity = await _configRepository.GetFirstAsync(x => x.Code == code);
            if (entity == null)
            {
                throw new Exception("此记录不存在！");
            }
            return entity;
        }


    }
}
